package com.controller;

import java.util.HashMap;
import java.util.Map;

import static com.HelloWorld.gson;
import static spark.Spark.*;

public class LoginController {
    public LoginController() { }

    public void init(){
        post("/login", (req,res) -> {
            String user = req.queryParams("user");
            String pass = req.queryParams("pass");

            System.out.println("user: "+user+" pass: "+pass);

            Map<String,String> result = new HashMap();

            if(user == null || pass == null){
                result.put("result","fail");
                return result;
            }

            if("admin".equals(user) && "1234".equals(pass)){
                System.out.println("登录成功: "+user);
                req.session().attribute("user","admin");
                result.put("user","admin");
                result.put("result","success");
                return result;
            }

            result.put("result","fail");

            return result;
        },gson::toJson);
    }
}
